<template>
  <div class="app-main-container">
    <el-tabs v-model="activeTab" @tab-click="getList(1)">
      <el-tab-pane label="员工通知" name="0" />
      <el-tab-pane label="平台信息" name="1" />
    </el-tabs>
    <QueryForm v-model="filters" :query-form-config="queryFormConfig" @handleQuery="getList(1)" @clear="clear" />
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="标题" prop="title" min-width="300" max-height="200">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="300" trigger="hover" :content="scope.row.title">
            <div slot="reference" class="single-maxheight">{{ scope.row.title }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="内容" prop="content" min-width="200" show-overflow-tooltip />
      <el-table-column v-if="activeTab == '0'" align="center" label="发布人员" prop="publishName" min-width="120" />
      <el-table-column v-if="activeTab == '1'" align="center" label="发布人员" prop="publishUser" min-width="120" />
      <el-table-column align="center" label="发布时间" prop="publishTime" min-width="160" />
      <el-table-column label="状态" prop="sort" min-width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.status === "1" ? "已读" : "未读" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="160" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('NoticeDetail') > -1"
              v-preventReClick
              type="text"
              @click="toDetail(scope.row.id, activeTab)"
            >查看详情</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" :page="pagination.page" @current-change="getList" @SizeChange="handleSizeChange" />
    <el-dialog title="详情" :visible.sync="isShowDetail" width="600px">
      <el-form ref="form" label-width="90px">
        <el-form-item label="标题：">
          <!-- <el-input v-model="detail.title" disabled /> -->
          <span>{{ detail.title }}</span>
        </el-form-item>
        <el-form-item label="发布时间：">
          <span>{{ detail.publishTime || detail.sendTime }}</span>
        </el-form-item>
        <el-form-item label="内容：">
          <!-- <el-input v-model="detail.content" disabled /> -->
          <span>{{ detail.content || '' }}</span>
        </el-form-item>
        <el-form-item v-if="detail.filePath" label="附件：">
          <a :href="detail.filePath"><el-button type="primary" size="small">查看附件</el-button></a>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { noticeList, noticeListb, noticeDetail, noticeDetailb } from '@/api'
import { parseTime } from '@/utils/index'
import Pagination from '@/components/paginations'
import QueryForm from '@/components/QueryForm'
export default {
  name: 'NoticeList',
  components: {
    QueryForm,
    Pagination
  },
  data() {
    return {
      queryFormConfig: {
        formItems: [{
          filed: 'name',
          type: 'input',
          label: '标题',
          placeholder: '请输入标题',
          clearable: true
        }],
        online: true
      },
      activeTab: '0',
      list: [],
      meta: [],
      listLoading: true,
      pagination: {
        page: 1,
        rows: 10
      },
      isShowDetail: false,
      detail: {},
      total: 0,
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  activated() {
    this.$refs.table.doLayout()
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    // 获取列表
    getList(page) {
      const { pagination, filters } = this
      const venueId = sessionStorage.getItem('venueId')
      if (page) {
        this.pagination.page = page
        pagination.page = page
      }
      pagination.firstQueryTime = parseTime(new Date())
      this.listLoading = true
      if (this.activeTab === '0') {
        noticeList({ pagination, param: { venueId, ...filters }})
          .then((res) => {
            this.listLoading = false
            if (res.errorCode !== '000000') return
            this.list = res.recordList
            this.total = res.totalCount
            if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
              this.getList(Math.ceil(res.totalCount / pagination.rows))
            }
            this.$refs.table.doLayout()
          })
          .catch(() => {
            this.listLoading = false
          })
      } else {
        noticeListb({ pagination, param: { venueId, ...filters }})
          .then((res) => {
            this.listLoading = false
            if (res.errorCode !== '000000') return
            this.list = res.recordList
            this.total = res.totalCount
            if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
              this.getList(Math.ceil(res.totalCount / pagination.rows))
            }
            this.$refs.table.doLayout()
          })
          .catch(() => {
            this.listLoading = false
          })
      }
    },
    toDetail(id, activeTab) {
      // console.log(id)
      // this.$router.push({
      //   name: 'NoticeDetail',
      //   query: {
      //     id,
      //     activeTab
      //   }
      // })
      if (activeTab === '1' || activeTab === 1) {
        noticeDetailb({
          param: {
            id
          }
        })
          .then((res) => {
            if (res.errorCode !== '000000') return
            this.isShowDetail = true
            this.detail = res.record
            this.getNotice()
          })
          .catch
      } else {
        noticeDetail({
          param: {
            id
          }
        })
          .then((res) => {
            if (res.errorCode !== '000000') return
            this.isShowDetail = true
            this.detail = res.record
            this.getNotice()
          })
          .catch(() => {
          })
      }
    },
    getNotice() {
      noticeList({
        param: {
          venueId: sessionStorage.getItem('venueId'),
          status: '0'
        }
      })
        .then((res) => {
          let num = res.totalCount
          if (num && num > 99) {
            num = 99
          }
          this.$store.dispatch('app/toSetNoticeNum', num)
        })
    },
    // 清空
    clear() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main-container {
  padding-top: 10px;
}

.tableClass .cell {
  padding: 0 !important;
  height: 40px;
  line-height: 30px !important;
  text-align: center;
}
</style>
